//
//  BPCategoryListViewController.m
//  BP_NewClient
//
//  Created by botao on 15/10/14.
//  Copyright © 2015年 normal. All rights reserved.
//  发型列表

#import "BPCategoryListViewController.h"
#import "BPListCellCollectionViewCell.h"
#import "UIImageView+WebCache.h"
#import "BURequestManager.h"

@interface BPCategoryListViewController ()

@end

@implementation BPCategoryListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.navigationItem.title = @"参考图片";
    self.navigationItem.leftBarButtonItem = BACK_BAR_BUTTON_BACK(@selector(back:));
    
    UICollectionViewFlowLayout *flowLayOut = [[UICollectionViewFlowLayout alloc] init];
    flowLayOut.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    collectView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - KNavBarHeight - 20) collectionViewLayout:flowLayOut];
    collectView.backgroundColor = [UIColor whiteColor];
    collectView.delegate = self;
    collectView.dataSource = self;
    [self.view addSubview:collectView];
    
    [collectView registerNib:[UINib nibWithNibName:@"BPListCellCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"BPListCellCollectionViewCell"];
    
    NSDictionary *dicParamer = [NSDictionary dictionaryWithObject:_category_id forKey:@"category_id"];
    // 请求数据
    [[BURequestManager sharedRequest] requestGetWithParameterWithBlockDic:FAXING_CATEGORY_LIST SetParamer:dicParamer success:^(NSDictionary *dic){
        _myDataArray = [dic objectForKey:@"data"];
        [collectView reloadData];
    } failure:^(NSString *errorMsg){
        [self showNoticeMsg:errorMsg];
    }];
}



#pragma mark -- UICollectionViewDataSource
//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _myDataArray.count;
}

//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * CellIdentifier = @"BPListCellCollectionViewCell";
    BPListCellCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

    NSDictionary *subModel = _myDataArray[indexPath.row];
    
    [cell.icon sd_setImageWithURL:[NSURL URLWithString:[subModel objectForKey:@"category_img"]] placeholderImage:[UIImage imageNamed:@"default-1"]];
    
    return cell;
}

#pragma mark --UICollectionViewDelegateFlowLayout

//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    collectionView.backgroundColor = [UIColor clearColor];
    return CGSizeMake(140, 140);
}

//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 10, 10, 10
                            );
}

#pragma mark --UICollectionViewDelegate

//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

}

//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionViewLayoutAttributes* attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    
    attributes.size = CGSizeMake(215/3.0, 303/3.0);
    attributes.center=CGPointMake(80*(indexPath.item+1), 62.5+125*indexPath.section);
    return attributes;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
